
import React, { Component } from 'react'
import axios from "axios"
export default class list extends Component {
    state={
        list:[]
    }
  async componentDidMount(){
        let res=await axios.get("/list")
        console.log(res.data);
        this.setState({
            list:res.data.res
        })
    }
    handclick(){
        this.props.history.push("/edit")
    }
   async del(id){
        let res=await axios.get("/del",{params:{id:id}})  
        console.log(res);
        if(res.data.code==0){
            let lis=await axios.get("/list")
            this.setState({
                list:lis.data.res
            })
            alert(res.data.mes)
        }
    }
    render() {
        let {list}=this.state
        return (
            <div className="list">
               <div className="list-top" onClick={this.handclick.bind(this)}>发表博客</div>
               <div>
                   {
                       list.map((item,index)=>{
                           return (
                               <div className="item" key={index}>
                                   <span className="title">{item.title}</span>
                                    <span>{item.text}</span>
                                    <span>{item.name}</span>
                                    <span>{item.img}</span>
                                    <span className="cz">
                                        <p onClick={()=>{this.del(item.id)}}>删除</p>
                                        <p>编辑</p>
                                    </span>
                               </div>
                           )
                       })
                   }
               </div>
            </div>
        )
    }
}
